layui.use(['table', 'form', 'laydate'], function () {
    var table = layui.table;
    var form = layui.form;
    var $ = layui.$
    var  demotable
    var gyerpUrls = jsRoutes.controllers.admin.SystemUsersController.osuser("gyerp");
    var kderpUrls = jsRoutes.controllers.admin.SystemUsersController.osuser("kderp");
    var kddcUrls = jsRoutes.controllers.admin.SystemUsersController.osuser("kddc");
     form.on('submit(jsonFormSubmit)', function (data) {
        //layer.msg(JSON.stringify(data.field));
        demotable = table.render({
            elem: '#demoTable'
            , headers: { "Csrf-Token": $('[name="csrfToken"]').val() }
            , contentType: 'application/json'
            , where: data.field
            , height: 550
            , method: gyerpUrls.method
            , url: gyerpUrls.url //数据接口
            , page: true //开启分页
            , limits: [10, 20, 50, 100, 200]
            , toolbar: 'default'
            , cols: [[ //表头
                { type: 'checkbox', fixed: 'left' }
                , { field: 'id', title: 'ID', width: 150, sort: true, fixed: 'left' }
                , { field: 'name', title: '名称', width: 120, sort: true }
                , { field: 'user', title: '员工', width: 120 }
                , { field: 'account', title: '账号', width: 180, sort: true }
                , { field: 'status', title: '状态', width: 80 }
                , { field: 'disable', title: '禁用时间', width: 180 }
                , { fixed: 'right', title: '操作', width: 200, align: 'center', toolbar: '#barDemo' }

            ]]
        });
        return false;
    });

    //监听头工具栏事件
    table.on('toolbar(test)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id)
            , data = checkStatus.data; //获取选中的数据
        switch (obj.event) {
            case 'add':
                layer.msg('添加');
                break;
            case 'update':
                if (data.length === 0) {
                    layer.msg('请选择一行');
                } else if (data.length > 1) {
                    layer.msg('只能同时编辑一个');
                } else {
                    layer.alert('编辑 [id]：' + checkStatus.data[0].id);
                }
                break;
            case 'delete':
                if (data.length === 0) {
                    layer.msg('请选择一行');
                } else {
                    layer.msg('删除');
                    console.log(data)
                }
                break;

        };
    });

    //监听行工具事件
    table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
        var data = obj.data //获得当前行数据
            , layEvent = obj.event; //获得 lay-event 对应的值
        if (layEvent === 'onoff') {

            console.log(data.id)
            $.post({
                   url: gyerpUrls.url,
                   contentType: 'application/json; charset=UTF-8',
                   headers: { "Csrf-Token": $('[name="csrfToken"]').val() },
                   data: JSON.stringify({ op: "onoff", sys: "gyerp", id: data.id,status: data.status }),
                   dataType: 'text',
                   success: function (res) {
                       console.log(res);
                       var d =  JSON.parse(res);
                       //alert("数据: \n" + data + "\n状态: " + status);
                       var s;
                       if (d.success){s = "成功"}else{s = "失败"}
                       layer.msg(data.name+'状态修改为'+d.status+'，操作'+s);
                       demotable.reload();
                   }
               });
        } else if (layEvent === 'del') {
            layer.confirm('真的删除行么', function (index) {
                layer.close(index);
                //向服务端发送删除指令
                console.log(data.id);
                $.post({
                url: gyerpUrls.url,
                   contentType: 'application/json; charset=UTF-8',
                   headers: { "Csrf-Token": $('[name="csrfToken"]').val() },
                   data: JSON.stringify({ op: "del", sys: "gyerp", id: data.id,status: data.status }),
                   dataType: 'text',
                   success: function (res) {
                       console.log(res);
                       var d =  JSON.parse(res);
                       //alert("数据: \n" + data + "\n状态: " + status);
                       var s;
                       if (d.success){s = "成功"}else{s = "失败"}
                       layer.msg(data.name+'状态修改为'+d.status+'，操作'+s);
                       demotable.reload();
                   }
                })
            });
        } else if (layEvent === 'edit') {
            //layer.msg('编辑操作');
            console.log(data);
//            $('.barDemoEdit').show();
            
            layer.open({
               type: 1
               , title: data.name
               , area: ['600px','400px']
               , shade: 0
               , content: $('.barDemoEdit')
               , btn: ['提交', '关闭']
               , yes: function (index, layero) {
                 //按钮【按钮一】的回调
                 console.log(index)
                 var fdata = form.val("barDemoEditForm")
                 if (data.user!="") fdata.user="0"
                 console.log(fdata)
                 var load_index = layer.load(1, { time: 30 * 1000 });
                 $.post({
                   url: gyerpUrls.url,
                   contentType: 'application/json; charset=UTF-8',
                   headers: {"Csrf-Token": $('[name="csrfToken"]').val()},
                   data: JSON.stringify({ op: "edit", sys: "gyerp",id:data.id,user:fdata.user,status: fdata.status}),
                   //                 dataType: 'text',
                   success: function (data) {
                     console.log(data);
                     layer.close(load_index);
                     layer.msg("数据: \n" + data + "\n状态: " + status);
                     demotable.reload();
                   }
                 });
                 layer.close(index);
               }
               , btn2: function (index, layero) {
                 //按钮【按钮二】的回调
                 //return false 开启该代码可禁止点击该按钮关闭
               }
               , cancel: function () {
                 //右上角关闭回调
                 //return false 开启该代码可禁止点击该按钮关闭
               }

             });
        }
    });


});